﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

namespace Controllers
{
    public class PerchaseController : BaseController
    {
        //分页查询
        public ActionResult Find(int start, int limit, string idStr,string name)
        {
            int empId;
            IList<int> deptIdList = getAuthedDeptIds("Perchase", idStr, out empId);
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;

            IQueryable<PerchaseDTO> q = from r in ctx.ReqPerchaseMains
                                           join s in ctx.SignFlows on
                                               new { r.FormNo, IsCurrent = 1 } equals new { s.FormNo, s.IsCurrent }
                                               into t
                                        from x in t.DefaultIfEmpty()
                                        where r.StatusId != 0 || r.EmpId == ebi.Id
                                           orderby r.FormNo descending
                                           select new PerchaseDTO
                                           {
                                               FormNo = r.FormNo,
                                               EmpId = r.EmpId,
                                               EmpName = r.Employee.Name,
                                               Cost = r.Cost,
                                               Cause = r.Cause,
                                               StatusId = r.StatusId,
                                               CheckEmpId = x.CheckEmpId
                                           };
            if (empId != 0)
            {
                q = q.Where(r => r.EmpId == empId);
            }
            else
            {
                q = q.Where(r => (from r1 in ctx.EmpDepPositions where deptIdList.ToArray().Contains(r1.DeptId) select r1.EmpId).Contains(r.EmpId));
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                q = q.Where(r => r.EmpName.Contains(name));
            }
            int totalCnt = q.Count();
            IList<PerchaseDTO> list = q.Skip(start).Take(limit).ToList();
            foreach (var dto in list)
            {
                var subList = from r in ctx.ReqPerchaseSubs where r.FormNo == dto.FormNo select r;
                string goodsName = null;
                foreach (var v in subList)
                {
                    goodsName += v.GoodsName;
                }
                dto.GoodsNames = goodsName;
            }
            //IQueryable<ReqVehicleMains> list = from r in ctx.ReqVehicleMains select r;
            CJson CJson = new CJson();
            string json = CJson.ToJsonString("totalCount:" + totalCnt + ",data", list);
            return Content(json);
        }

        //删除明细
        public string DelDetail(int id)
        {
            string retStr = "Succeed";
            ReqPerchaseSub oriObj = ctx.ReqPerchaseSubs.Single(r => r.Id == id);
            ctx.ReqPerchaseSubs.DeleteOnSubmit(oriObj);
            try
            {
                ctx.SubmitChanges();
            }
            catch (Exception ex)
            {
                retStr = ex.Message;
            }
            return retStr;
        }

        //删除申请,含明细
        public ActionResult Delete(string formNo)
        {
            var rdto = new ResultMessages();
            rdto.Message = "";
            ReqPerchaseMain oriObj = ctx.ReqPerchaseMains.Single(r => r.FormNo == formNo);
            ctx.ReqPerchaseSubs.DeleteAllOnSubmit(oriObj.ReqPerchaseSubs);
            ctx.ReqPerchaseMains.DeleteOnSubmit(oriObj);
            try
            {
                ctx.SubmitChanges();
                rdto.Message = "删除成功";
                rdto.Result = true;
            }
            catch
            {
                rdto.Message = "删除失败";
                rdto.Result = false;
            }
            return Json(rdto);
        }
    }
}